//
//  HQHNewWriteCollectionViewCell.m
//  LastOne
//
//  Created by 小柠檬 on 15/11/6.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "HQHNewWriteCollectionViewCell.h"
#import "HTTPMethod.h"
#import "ChangeString.h"
#import "HQHFreeWriteModel.h"
#import "HQHFreeDetailTableViewCell.h"
#import "HQHFreeDetailSecondTableViewCell.h"
#import <MJRefresh.h>
#import "FitHeader.h"
#import "NightHeader.h"
#import "GiFHUD.h"
@implementation HQHNewWriteCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.DataSource = [NSMutableArray array];
        self.listArray = [NSMutableArray array];
        
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 554 * FitHeight)];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        _tableView.tableFooterView = [[UITableView alloc]init];
        
        [self.contentView addSubview:self.tableView];
        [GiFHUD setGifWithImageName:@"loading.gif"];
        
        _page = 1;
        [self getData];
        
        _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [_DataSource removeAllObjects];
            _page = 1;
            [self getData];
        }];
        
        _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            _page++;
            [self getData];
        }];
        
        
        
    }
    return self;
}

-(void)getData
{
    [GiFHUD show];
    
    [HTTPMethod getDataByString:@"http://api2.pianke.me/read/latest" BodyDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"auth=&client=1&deviceid=137794B8-2578-4119-B189-3E532E7C0F5B&limit=10&sort=addtime&start=%ld&version=3.0.6", _page * 10]] WithDataBlock:^(id data) {
        NSDictionary *dataDic = [data objectForKey:@"data"];
        NSArray *array = [dataDic objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            [_listArray addObject:dic];
            HQHFreeWriteModel *freeWriteModel = [[HQHFreeWriteModel alloc]init];
            [freeWriteModel setValuesForKeysWithDictionary:dic];
            [self.DataSource addObject:freeWriteModel];
            
        }
        
        [self.tableView reloadData];
        [GiFHUD dismiss];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
    }];
}





-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160 * FitHeight;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _DataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[_listArray[indexPath.row] objectForKey:@"firstimage"] isEqualToString:@""]) {
            static NSString *identifier = @"cell";
            HQHFreeDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (!cell) {
                cell = [[HQHFreeDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (_DataSource.count != 0) {
                cell.freeWriteModel = _DataSource[indexPath.row];
            }
            [cell NightWithType:UIViewColorTypeNormal];
            return cell;
        }
        else
        {
            static NSString *identifier2 = @"cell2";
            HQHFreeDetailSecondTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:identifier2];
            if (!cell2) {
                cell2 = [[HQHFreeDetailSecondTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier2];
            }
            cell2.selectionStyle = UITableViewCellSelectionStyleNone;
            if (_DataSource.count != 0) {
                cell2.freeWriteModel = _DataSource[indexPath.row];
            }
            [cell2 NightWithType:UIViewColorTypeNormal];
            return cell2;
        }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = [_DataSource[indexPath.row]contentid];
    [self.myNewWriteDelegate passWriteNewValue:_DataSource[indexPath.row] With:str];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
